import ColumnGroup from 'primevue/columngroup'
import { LANGUAGE } from './config'

function load(lang) {
  const languages = Object.keys(LANGUAGE)
  for (let l of languages) {
    if (!Array.isArray(LANGUAGE[l])) {
      continue
    }
    if (LANGUAGE[l][0] === lang) {
      let language = {}
      for (let i = 1; i < LANGUAGE[l].length; i++) {
        language = { ...language, ...LANGUAGE[l][i] }
      }
      return language
    }
  }
  return LANGUAGE.EN[1]
}

export function switchLanguage(lang = LANGUAGE.EN[0]) {
  const language = load(lang)
  const translate = (key, content = '') => {
    let translated = language[key]
    if (typeof translated === 'function') {
      translated = translated(content)
    }
    if (!translated && translated !== '') {
      return key
    }
    return translated
  }
  translate.language = lang
  return translate
}
